from dao import Dao
from model.ticket import Ticket
from model.user import User
from bson.dbref import DBRef

class TicketDao(Dao):
    """This the base class for AdvertisementDao, PictureDao
    """
    def __init__(self):
        Dao.__init__(self, Ticket)
        self.collection = 'tickets'
        
        
    def save(self, model):
        """Updates an existing document or inserts a new document, depending on its document parameter.
        Arguments:
            model --- The Model object with id,  if model is a dict type, the dbref field must be DBRef type
        Return:
            The '_id' value of to_save or [None] if manipulate is False and to_save has no _id field.
        """
        # The ID field must exist
        if isinstance(model, dict):
            return self.db.save(self.collection, model)
        else:
            _dict = model.__dict__
            if isinstance(model.user, User):
                _dict['user'] = DBRef('users', model.user._id)
            else:
                _dict['user'] = None # For root
            return self.db.save(self.collection, _dict)
        
